import datetime

__author__ = 'NCS-2'

import os
import ast
from main.models import *
import json


def load_data():
    # print os.path.abspath(__file__)
    path_dir = os.path.dirname(os.path.abspath(__file__)) + "/../data/"

    # print os.listdir(path_dir).__len__();


    # create Nhom Nganh & Nganh & Choices
    # file = open(path_dir+'nhom_nganh.json')
    # contents = json.load(file)
    #
    # for choice in choices:
    # choices = Choices.LUA_CHON_KHOI_THI
    # choice_new = Choices(description=choice)
    # choice_new.save()
    #
    # for nhom_nganh_lon in contents['nhom_nganh']:
    # for nhom_nganh_nho in nhom_nganh_lon['list_nganh']:
    #
    #         ma_nhom_nganh = ""
    #         ten_nhom_nganh = ""
    #
    #         if 'ma_nganh' in nhom_nganh_nho:
    #             ma_nhom_nganh = nhom_nganh_nho['ma_nganh']
    #
    #         if 'ten_nganh' in nhom_nganh_nho:
    #             ten_nhom_nganh = nhom_nganh_nho['ten_nganh']
    #
    #         print ma_nhom_nganh, ten_nhom_nganh
    #
    #         if not Nhom_Nganh.objects.filter(ma_nhom_nganh=ma_nhom_nganh) and ma_nhom_nganh is not "":
    #             nhom_nganh = Nhom_Nganh(ma_nhom_nganh=ma_nhom_nganh,ten_nhom_nganh=ten_nhom_nganh)
    #             nhom_nganh.save()
    #
    #             for nganh in nhom_nganh_nho['list_nganh']:
    #                 ten_nganh = ""
    #                 ma_nganh = ""
    #
    #                 if 'ten_nganh' in nganh:
    #                     ten_nganh = nganh['ten_nganh']
    #
    #                 if 'ma_nganh' in nganh:
    #                     ma_nganh = nganh['ma_nganh']
    #
    #                 print ma_nganh, ten_nganh
    #
    #                 if not Nganh.objects.filter(ma_nganh=ma_nganh) and ma_nganh is not "":
    #                     nganh = Nganh(ma_nganh=ma_nganh,ten_nganh=ten_nganh,nhom_nganh=nhom_nganh)
    #                     nganh.save()
    #
    # file.close()


    # create Other Information
    for file in os.listdir(path_dir):
        if file.endswith(".txt"):
            file_full = path_dir + file
            with open(file_full) as f:
                contents = f.readlines()

                ten_truong = contents[0]

                end = file.find(".txt")
                start = end - 3
                ma_truong = file[start:end]

                # print ma_truong

                # if not Truong.objects.filter(ma_truong=ma_truong) and ma_truong is not "":
                #     truong = Truong(ten_truong=ten_truong, ma_truong=ma_truong)
                #     truong.save()
                #
                # if not Khoa.objects.filter(ma_khoa=ma_truong + '_000') and ma_truong is not "":
                #     truong = Truong.objects.get(ma_truong=ma_truong)
                #     khoa = Khoa(ten_khoa='Default', ma_khoa=ma_truong + '_000', truong=truong)
                #     khoa.save()

                # create choices


                for content in contents:
                    if contents.index(content) == 0:
                        continue

                    ten_nganh = ""
                    ma_nganh = ""
                    ghi_chu = ""
                    khoi_thi = ""
                    diem_chuan = 0
                    diem_nv2 = 0
                    chi_tieu_nv2 = 0

                    format_content = content.replace("null", "\"\"")
                    json_data = ast.literal_eval(format_content)


                    # all_keys = all_keys.union(set(json_data.keys()))

                    # attributes that always have
                    if "ten_nganh" in json_data:
                        ten_nganh = json_data["ten_nganh"].decode('unicode_escape')
                    if "ma_nganh" in json_data:
                        ma_nganh = json_data["ma_nganh"].decode('unicode_escape')
                    if "khoi_thi" in json_data:
                        khoi_thi = json_data["khoi_thi"].decode('unicode_escape')
                    if "ghi_chu" in json_data:
                        ghi_chu = json_data["ghi_chu"].decode('unicode_escape')
                    if "diem_chuan" in json_data:
                        diem_chuan = float(json_data["diem_chuan"].decode('unicode_escape'))

                    # attributes that are optional

                    if "diem_nv2" in json_data:
                        diem_nv2 = float(json_data["diem_nv2"].decode('unicode_escape'))
                    if "chi_tieu_nv2" in json_data:
                        chi_tieu_nv2 = int(json_data["chi_tieu_nv2"].decode('unicode_escape'))

                    # #print ten_truong, ten_nganh, diem_chuan
                    # print ma_nganh, ma_truong+'_000'

                    khoi_thi_danh_sach = []
                    if khoi_thi is not "" and ma_nganh is not "" and ten_nganh is not "" and diem_chuan is not 0:
                        try:
                            nganh = Nganh.objects.filter(ma_nganh=str(ma_nganh)[1:])[0]
                            khoa = Khoa.objects.filter(ma_khoa=ma_truong + '_000')[0]

                            khoi_thi_list = khoi_thi.split(',')
                            for khoi_thi_nho in khoi_thi_list:
                                if khoi_thi_nho in dict(Choices.LUA_CHON_KHOI_THI).keys():
                                    try:
                                        index = dict(Choices.LUA_CHON_KHOI_THI).keys().index(khoi_thi_nho)
                                        k1 = Choices.objects.all()[index]
                                        khoi_thi_danh_sach.append(k1)
                                        # print index
                                    except Choices.DoesNotExist:
                                        print 'error Choices DoesNotExist', khoi_thi_nho


                            # print khoi_thi_danh_sach, khoa, nganh

                            if Dao_Tao.objects.filter(khoa=khoa, nganh=nganh).count() is 0:
                                dao_tao = Dao_Tao(khoa=khoa, nganh=nganh)
                                # dao_tao.save()
                                #                   # diem_chuan=diem_chuan, diem_chuan_nv2=diem_nv2, chi_tieu_nv2=chi_tieu_nv2,
                                #                   # ghi_chu=ghi_chu, nam_tuyen_sinh=datetime.datetime(year=2013,month=8,day=1))
                                #
                                dao_tao.diem_chuan=diem_chuan
                                dao_tao.diem_chuan_nv2=diem_nv2
                                dao_tao.chi_tieu_nv2=chi_tieu_nv2
                                dao_tao.ghi_chu=ghi_chu
                                dao_tao.nam_tuyen_sinh = datetime.datetime(year=2014,month=8,day=1)
                                dao_tao.save()

                                if len(khoi_thi_danh_sach) is not 0:
                                    print 'still go here', ma_nganh, ma_truong, khoi_thi_danh_sach
                                    dao_tao.khoi_thi = khoi_thi_danh_sach

                                dao_tao.save()


                        except Khoa.DoesNotExist:
                            pass
                        except Nganh.DoesNotExist:
                            pass
                        except IndexError:
                            print 'error IndexError', ma_nganh, ma_truong
                        except UnicodeEncodeError:
                            print 'error UnicodeEncodeError', ma_nganh, ma_truong
                        except UnicodeDecodeError:
                            print 'error UnicodeDecodeError', ma_nganh, ma_truong, khoi_thi_danh_sach


load_data()
